//移动零
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        //nums分为3块：
        //[0,dest]:表示已处理元素(全为非0)
        //[dest+1,cur-1]：已处理且为0的元素
        //[cur,n-1]：待处理元素
        //双指针移动期间必须一直保持上述性质
        int n = nums.size();
        int dest = -1;
        for(size_t cur = 0 ; cur < n ; ++cur)
        {
            if(nums[cur])
                swap(nums[++dest],nums[cur]);
        }

    }
};

//复写零
class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int cur = 0 , dest = -1 , n = arr.size();
        while(cur < n)
        {
            if(arr[cur]) dest++;
            else dest+=2;
            if(dest >= n-1)
                break;
            cur++;
        }
        if(dest == n)
        {
            arr[n-1] = 0;
            dest-=2;
            cur--;
        }

        while(cur >= 0)
        {
            if(arr[cur]) arr[dest--] = arr[cur--];
            else 
            {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }
};

//快乐数
class Solution {
public:
    int Sum(int num)
    {
        int sum = 0;
        while(num > 0)
        {
            int temp = num % 10;
            sum += (temp*temp);
            num/=10;
        }
        return sum;
    }

    bool isHappy(int n) {
        int slow = n , fast = Sum(n);
        int count = 0;
        while(slow !=fast)
        {
            slow = Sum(slow);
            fast = Sum(Sum(fast));
            count++;
            std::cout << count << std::endl;
        }
        if(slow == fast && slow == 1)
            return true;
        return false;
    }
};

//盛最多水的容器
class Solution {
public:
    int maxArea(vector<int>& heightArr) {
        int begin = 0 , n = heightArr.size() , end = n-1;
        int ret = 0;
        while(begin < end)
        {
            int len = end-begin;
            int height = min(heightArr[begin],heightArr[end]);
            int capacity = len*height;
            ret = max(capacity,ret);
            heightArr[begin] > heightArr[end] ? end-- : begin++;
        }
        return ret;
    }
};

//有效三角形的个数
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        //双指针+单调性
        sort(nums.begin(),nums.end());
        int n = nums.size();
        int ret = 0;
        for(int i = n-1 ; i >= 2 ; --i)
        {
            int left = 0 , right = i-1;
            while(left < right)
            {
                if(nums[right] + nums[left] > nums[i])
                {
                    ret += right - left;
                    --right;
                }
                else 
                {
                    ++left;
                }
            }
        }
        return ret;
    }
};

//查找总价格为目标值的两个商品
class Solution {
public:
    vector<int> twoSum(vector<int>& price, int target) {
        int n = price.size() , left = 0 , right = n-1;
        vector<int> ret(2);
        while(left < right)
        {
            if(price[left] + price[right] > target)
            {
                --right;
            }
            else if(price[left] + price[right] < target)
            {
                ++left;
            }
            else 
            {
                ret[0] = price[left];
                ret[1] = price[right];
                break;
            }
        }
        return ret;
    }
};

//三数之和
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        //利用双指针+单调性
        int n = nums.size();
        sort(nums.begin(),nums.end());
        int i = n-1;
        while(i >= 2)
        {
            //固定下标为i的元素
            int left = 0;
            int right = i-1;
            while(left < right)
            {
                int sum = nums[left] + nums[right] + nums[i];
                if(sum < 0)
                {
                    left++;
                }
                else if(sum > 0)
                {
                    right--;
                }
                else 
                {
                    ret.push_back({nums[left],nums[right],nums[i]});
                    ++left;
                    --right;
                    while(left < right && nums[left-1] == nums[left])
                        ++left;
                    while(left < right && nums[right+1] == nums[right])
                        --right;
                }
            }
            --i;
            while(i > 0 && nums[i+1] == nums[i]) --i;
        }

        return ret;
    }
};
